package edu.metrostate.capstonediner;

import android.os.Bundle;
import android.os.Looper;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.os.Build;

public class PaymentActivity extends Activity implements Runnable {
	
	private PaymentActivity myInstance;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_payment);
		// Show the Up button in the action bar.
		setupActionBar();
		
		myInstance = this;
        
        // Buttons
        Button btnSavePayment = (Button) findViewById(R.id.savePaymentButton);
        
        // Login with the username and password
        btnSavePayment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            	new Thread(myInstance).start();
            }
        });
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.payment, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void run() {
		String user = GameActivity.getUser();
    	String firstName = ((EditText)findViewById(R.id.cardFirstNameText)).getText().toString();
    	String lastName = ((EditText)findViewById(R.id.cardLastNameText)).getText().toString();
    	String cardNumber = ((EditText)findViewById(R.id.cardNumberText)).getText().toString();
    	String month = ((EditText)findViewById(R.id.cardMonthText)).getText().toString();
    	String year = ((EditText)findViewById(R.id.cardYearText)).getText().toString();
    	String street = ((EditText)findViewById(R.id.cardStreetText)).getText().toString();
    	String city = ((EditText)findViewById(R.id.cardCityText)).getText().toString();
    	String state = ((EditText)findViewById(R.id.cardStateText)).getText().toString();
    	String zip = ((EditText)findViewById(R.id.cardZipText)).getText().toString();
    	
    	Looper.prepare();
    	AlertDialog.Builder builder = new AlertDialog.Builder(myInstance);
    	
    	if (user. length() > 0 && firstName.length() > 0 && lastName.length() > 0 && cardNumber.length() > 0 && month.length() > 0 && year.length() > 0 && street.length() > 0 && city.length() > 0 && state.length() > 0 && zip.length() > 0) {
	    	if (DatabaseHTTP.SavePayment(user, firstName, lastName, cardNumber, month, year, street, city, state, zip)) {
	    		builder.setMessage("Congratulations!  Your payment method has been successfully created.");
	    		builder.setPositiveButton("Okay!", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						myInstance.finish();
					}
				});
	            builder.create().show();
	            Looper.loop();
	    	} else {
	            builder.setMessage("Failed to create payment method.  Please try again.");
	            builder.create().show();
	            Looper.loop();
	    	}
    	} else {
    		builder.setMessage("Please make sure that you fill in all of the fields.");
            builder.create().show();
            Looper.loop();
    	}
	}

}
